
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports _2bMatesSite.Common


Namespace _2bMatesSite.BO
	''' <summary>
	''' Blog message.
	''' </summary>
	Public Class Blog
		Inherits BaseBlog
		Public Sub New(ByVal dr As SqlDataReader)
			MyBase.New(dr)
		End Sub

		' Use to post blog comment
		Public Sub New(ByVal parentID As Integer, ByVal fromUID As Integer, ByVal subject As String, ByVal body As String, ByVal approved As Boolean)
			MyBase.New(parentID, fromUID, subject, body)
			Me.status = IIf(approved,BlogStatus.gbsApproved,BlogStatus.gbsInitial)
		End Sub

		' Use to post new blog
		Public Sub New(ByVal fromUID As Integer, ByVal subject As String, ByVal body As String)
			MyBase.New(fromUID, subject, body)
			Me.status = BlogStatus.gbsApproved
		End Sub

		Public Overloads Overrides Sub Delete()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				Dim cmd As SqlCommand

				If parentID < 0 Then
					' It's a root blog - delete all children first
					cmd = New SqlCommand("DELETE FROM Journal WHERE ParentID=@ParentID")
					cmd.Connection = conn
					cmd.Transaction = tran
					cmd.Parameters.Add("@ParentID", blogID)
					cmd.ExecuteNonQuery()
				End If

				cmd = New SqlCommand("DELETE FROM Journal WHERE ID=@ID")
				cmd.Connection = conn
				cmd.Transaction = tran
				cmd.Parameters.Add("@ID", blogID)
				cmd.ExecuteNonQuery()

				tran.Commit()
			Catch generatedExceptionName As Exception
				tran.Rollback()
				Throw
			Finally
				conn.Close()
			End Try
		End Sub

		Public Overloads Overrides Sub Save()
			If blogID < 0 Then
				Throw New MessageException("New Blog entries may not be modified. Use Send to insert a new entry.")
			End If

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Dim tran As SqlTransaction = conn.BeginTransaction()

			Try
				Dim cmd As New SqlCommand("UPDATE Journal SET Subject=@Subject, Body=@Body, LastUpdated=@LastUpdated, Status=@Status WHERE ID=@ID")
				cmd.Connection = conn
				cmd.Transaction = tran

				cmd.Parameters.Add("@ID", blogID)

				cmd.Parameters.Add("@Subject", subject)
				cmd.Parameters.Add("@Body", body)
				cmd.Parameters.Add("@LastUpdated", DateTime.Now)
				cmd.Parameters.Add("@Status", DirectCast(status, Integer))

				cmd.ExecuteNonQuery()
				tran.Commit()
			Catch generatedExceptionName As Exception
				tran.Rollback()
				Throw
			Finally
				conn.Close()
			End Try

			If parentID <> -1 Then
				' Send the blog owner a notification
				Dim blogOwner As User = Messages.GetBlogEntry(parentID).Sender
				If blogOwner.ID <> senderUID Then
					Users.SendBlogUpdateNotification(blogOwner, Me)
				End If
			End If
		End Sub

		Public Overloads Overrides Sub Send()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand("SaveBlog")
				cmd.CommandType = CommandType.StoredProcedure
				cmd.Connection = conn

				cmd.Parameters.Add("@UID", senderUID)
				DBManager.SetNZValue(cmd, "@ParentID", parentID)
				cmd.Parameters.Add("@Subject", subject)
				cmd.Parameters.Add("@Body", body)
				cmd.Parameters.Add("@Date", DateTime.Now)
				cmd.Parameters.Add("@Status", DirectCast(status, Integer))

				Dim paramID As SqlParameter = cmd.Parameters.Add("@ID", SqlDbType.Int)
				paramID.Direction = ParameterDirection.Output

				cmd.ExecuteNonQuery()

				blogID = DirectCast(paramID.Value, Integer)
			Finally
				conn.Close()
			End Try

			If parentID <> -1 Then
				' Send the blog owner a notification
				Dim blogOwner As User = Messages.GetBlogEntry(parentID).Sender
				If blogOwner.ID <> senderUID Then
					Users.SendBlogNotification(blogOwner, Me)
				End If
			End If
		End Sub

		'
		' Private methods
		'

		Protected Overloads Overrides Sub Init(ByVal dr As SqlDataReader)
			blogID = dr.GetInt32(dr.GetOrdinal("ID"))
			senderUID = dr.GetInt32(dr.GetOrdinal("UID"))
			parentID = DBManager.GetNZInt32(dr, "ParentID")
			subject = dr.GetString(dr.GetOrdinal("Subject"))
			body = dr.GetString(dr.GetOrdinal("Body"))
			dateSent = dr.GetDateTime(dr.GetOrdinal("Date"))
			status = DirectCast(dr.GetInt32(dr.GetOrdinal("Status")), BlogStatus)
		End Sub
	End Class
End Namespace






